import { defineStore } from 'pinia'
import LoginHandle from '@/func/login'
import { ApiMbrUsr, ApiMbrAuth } from "@/api/member.js"
import UserHandle from "@/func/user"
import avatar from "@/assets/images/avatar.png"
import { ls } from '@/utils/storage.js'
import paymentHandler from "@/func/payment";
export const useAppStore = defineStore({
    id: 'app',
    state: () => ({
        token: localStorage.getItem("token") || '',
        isAuth: ls.get(ls.isAuth),    //用户是否认证
        userInfos: {},                   // 用户信息
        carNumber: 0,                    // 购物车数量
        walletBalance: {
            balance: 0,
            incomeActivity: 0,
            incomeCopyright: 0,
            incomePrice: 0,
            totalExpense: 0,
            totalRecharge: 0
        },                      // 余额
    }),
    getters: {},
    actions: {
        // 获取用户余额
        getBalance() {
            return new Promise((resolve, reject) => {
                paymentHandler.GetWallet()
                    .then((res) => {
                        if (res.code == 0) {
                            this.walletBalance = res.data
                            resolve(res);
                        } else {
                            reject(res);
                        }
                    })
                    .catch((error) => { })
            })
        },
        // 获取用户信息
        getUserInfo() {
            return new Promise((resolve, reject) => {
                ApiMbrUsr.R()
                    .then((res) => {
                        this.userInfos = {
                            ...res.data,
                            avatar: res.data.avatar || avatar
                        }
                        resolve(res)
                    })
                    .catch((error) => {
                        reject(error)
                    })
            })
        },
        // 获取用户是否认证
        getAuthInfo() {
            return new Promise((resolve, reject) => {
                ApiMbrAuth.Chk()
                    .then((res) => {
                        if (res.code == 0) {
                            this.isAuth = res.data;
                            ls.set(ls.isAuth, res.data);
                            resolve(res);
                        } else {
                            reject(res);
                        }
                    })
                    .catch((error) => {
                        reject(error)
                    })
            })
        },
        // 获取购物车数量
        getCarCount() {
            return new Promise((resolve, reject) => {
                UserHandle.CartCount()
                    .then((res) => {
                        if (res.code == 0) {
                            this.carNumber = res.data
                            resolve(res);
                        } else {
                            ElMessage.error(res.msg);
                            reject(res);
                        }
                    })
                    .catch((error) => {
                        reject(error)
                    })
            })
        },
        Login(user) {
            var mobile = user.mobile.trim();
            var code = user.code.trim();
            var bd_vid = user.bd_vid?user.bd_vid:'';
            return new Promise((resolve, reject) => {
                LoginHandle.PostPhoneLogin({ mobile, code, bd_vid }).then((res) => {
                    if (res.code == 0) {
                        localStorage.setItem("token", res.data.accessToken);
                        this.token = res.data.accessToken
                        resolve(res);
                    } else {
                        ElMessage.error(res.msg);
                        reject(res);
                    }
                }).catch(err => {
                    reject(err);
                });
            });

        },
        passwordLogin(user) {
            var mobile = user.mobile.trim();
            var password = user.password.trim();
            var bd_vid = user.bd_vid?user.bd_vid:'';
            return new Promise((resolve, reject) => {
                LoginHandle.PostPhonePasswordLogin({ mobile, password, bd_vid }).then((res) => {
                    if (res.code == 0) {
                        localStorage.setItem("token", res.data.accessToken);
                        this.token = res.data.accessToken
                        resolve(res);
                    } else {
                        ElMessage.error(res.msg);
                        reject(res);
                    }

                }).catch(err => {
                    reject(err);
                });
            });

        },
        QrLogin(user) {
            var ticket = user.ticket;
            var bd_vid = user.bd_vid?user.bd_vid:'';
            return new Promise((resolve, reject) => {
                LoginHandle.QrCodeLogin({ ticket, bd_vid }).then((res) => {
                    if (res.code == 0) {
                        localStorage.setItem("token", res.data.accessToken);
                        this.token = res.data.accessToken
                        resolve(res);
                    } else {
                    }

                })
            });
        },
        // 邮箱登录
        emailLogin(user) {
            var email = user.email.trim();
            var emailCode = user.emailCode.trim();
            var bd_vid = user.bd_vid?user.bd_vid:'';
            return new Promise((resolve, reject) => {
                LoginHandle.emailLogin({ email, emailCode, bd_vid }).then((res) => {
                    if (res.code == 0) {
                        localStorage.setItem("token", res.data.accessToken);
                        this.token = res.data.accessToken
                        resolve(res);
                    } else {
                        ElMessage.error(res.msg);
                        reject(res);
                    }
                }).catch(err => {
                    reject(err);
                });
            });
        },
        Logout(user) {
            var username = user.username;
            return new Promise((resolve, reject) => {
                AuthHandler.Logout({ username }).then((res) => {
                    if (res.code == 0) {
                        ls.rm(ls.token);
                        this.token = ''
                        resolve(res);
                    } else {
                        reject(new Error(res.msg));
                    }
                }).catch(err => {
                    reject(err);
                });
            });
        },

        // 前端登出
        fedLogout() {
            ls.rm(ls.token);
            ls.rm(ls.isAuth);
            this.token = ''
            this.isAuth = false
        },
    }
})

// export default useAppStore
